package com.woniuxy.processweb.controller;


import com.woniuxy.core.model.Result;
import com.woniuxy.core.util.MyJwtUtil;
import com.woniuxy.core.util.PageInfo;
import com.woniuxy.processdao.model.Teacard;
import com.woniuxy.processmodel.dto.TeaCardDto;
import com.woniuxy.processmodel.form.AddTeacardForm;
import com.woniuxy.processmodel.form.UpdateTeacardForm;
import com.woniuxy.processmodel.form.VenueLookTeacardForm;
import com.woniuxy.processservice.exception.ProcessException;
import com.woniuxy.processservice.service.TeacardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 团课流程表 前端控制器
 * </p>
 *
 * @author 冉杰
 * @since 2024-01-15
 */
@RestController
@RequestMapping("/teacard")
@Api(tags = {"团课进度记录接口"})
public class TeacardController {
    @Autowired
    private TeacardService teacardService;

    @ApiOperation("新增团课进度接口")//团课订单生成时调用该接口
    @GetMapping("/add")
    public Result addTeacard(String orderNum){
        teacardService.addTeacard(orderNum);
        return Result.success("新增团课进度成功");
    }

    @ApiOperation("修改团课进度接口")//每完成一节团课后，商家调用此接口进行进度的记录
    @PostMapping("/update.venue")
    public Result updateTeacard(@RequestBody UpdateTeacardForm form, @RequestHeader String token){
        Integer venueId = MyJwtUtil.getVenueIdByKey(token, "venueId");
        teacardService.updateTeacard(form,venueId);
        return Result.success("修改私教课进度成功");
    }
    @ApiOperation("根据id查询团课进度接口")
    @GetMapping("/look")
    public Result lookTeacard(Integer teacardId){
        Teacard teacard = teacardService.lookTeacard(teacardId);
        return Result.success("查询团课进度成功",teacard);
    }
    @ApiOperation("用户查询自己的团课进度接口")
    @GetMapping("/userLook")
    public Result userLookTeacard(String orderNum,@RequestHeader String token){
        Integer userId = MyJwtUtil.getUserIdByKey(token, "userId");
        List<TeaCardDto> dtos = teacardService.userLookPricard(orderNum,userId);
        return Result.success("用户查询团课进度成功",dtos);
    }

    @ApiOperation("商家查询自己的团课进度接口")
    @PostMapping("/venueLook.venue")
    public Result venueLookTeacard(@RequestBody @Validated VenueLookTeacardForm form, BindingResult br, @RequestHeader String token){
        if(br.hasErrors()) throw new ProcessException(90001,"参数校验异常");
        Integer venueId = MyJwtUtil.getVenueIdByKey(token, "venueId");
        PageInfo<TeaCardDto> dtos = teacardService.venueLookTeacard(form,venueId);
        return Result.success("查询团课进度成功",dtos);
    }

}

